package util.validate
{
	import flash.geom.Rectangle;
	
	import mx.controls.TextInput;
	import mx.controls.ToolTip;
	import mx.events.ValidationResultEvent;
	import mx.managers.ToolTipManager;
	import mx.validators.Validator;

	public class ValidationManager
	{
		[Bindable]
		private var validatorArr:Array=new Array();//保存所有的validator
		
		private var allErrorTips:Array;
		
		private var stage:Object;
		
		public function ValidationManager(stage:Object=null):void{
			this.stage=stage;
		}
		
		public function addValidator(validator:Validator):void {
			validatorArr.push(validator);
		}
		
		public function doValidate():void{
			var validatorErrorArray:Array = Validator.validateAll(validatorArr);
//			var isValid:Boolean = validatorErrorArray.length == 0;
//			if (!isValid) {
//				var err:ValidationResultEvent;
//				for each (err in validatorErrorArray) {
//					var pt:Rectangle = stage.stage.getBounds(err.currentTarget.source);
//					var yPos:Number = pt.y * -1;
//					var xPos:Number = pt.x * -1;
//					// now create the error tip
//					var errorTip:ToolTip = ToolTipManager.createToolTip(err.message, xPos + err.currentTarget.source.width, yPos, 'errorTipRight') as ToolTip;
//					// store the error tip so we can remove it later when
//					// the user revalidates
////					allErrorTips.push(errorTip);
//				}
//			}
			
		}
		
		public function validateAll(validators:Array):Array
		{   
			var result:Array = [];
			
			var n:int = validators.length;
			for (var i:int = 0; i < n; i++)
			{
				var v:Validator = Validator(validators[i]);
				
				if (v.enabled)
				{
					var resultEvent:ValidationResultEvent = v.validate(null,true);
					
					if (resultEvent.type != ValidationResultEvent.VALID)
						result.push(resultEvent);
				}
			}   
			
			return result;
		}
	}
}